const dataBase = require('../database')

// 添加商品
function addGoods(req, res){
    const goods = req.body.goods

    // 定义一条添加商品的 SQL
    const addGoodsSql = 'insert into goods(goodsname, goodsprice, kucun, goodsInfo, goodstype,  goodsImg, AddData, AddActor) value (?, ?, ?, ?, ?, ?, ?, ?)'
    
    // 判断信息是否填写完成
    if(goods.goodsname === '' || goods.goodsInfo === '' || goods.goodstype === '' || goods.goodsprice === '' || goods.goodsImg === '' || goods.kucun === null){
        return res.send({
            status: 404,
            message: '请将信息填写完整！'
        })
    }

    // 执行 SQL 进行商品的添加
    dataBase.query(addGoodsSql, [goods.goodsname, goods.goodsprice, goods.kucun, goods.goodsInfo, goods.goodstype, goods.goodsImg, goods.AddData, goods.AddActor], (err, results) => {
        if(err){
            return res.send({
                status: 404,
                message: '未知错误！请稍后重试！'
            })
        }
        if(results.affectedRows === 1){
            res.send({
                status: 200,
                message: '添加成功！'
            })
        }
    })
}


// 获取前 7 热门商品
function getPopularGoods(req, res){

    // 定义一条根据点击量降序查询的所有商品
    const getPopularGoodsSql = 'select * from goods order by PurchaseQuantity DESC'

    dataBase.query(getPopularGoodsSql,(err, results) => {
        if(err){
            return res.send({
                status: 404,
                message: '未知错误！请稍后重试！'
            })
        }

        if(results.length <= 0){
            return res.send({
                status: 404,
                message: '暂无数据！'
            })
        }

        res.send({
            status: 200,
            message: '获取成功！',
            data: results.slice(0, 7)
        })
    })
}


// 获取走马灯信息
function getBigGoodsInfo(req, res){
    // 定义一个查询所有商品的  SQL
    const getBigGoodsInfoSql = 'select * from goodsbigimg left join goods g on g.id = goodsbigimg.goodsId'

    // 执行 SQL 
    dataBase.query(getBigGoodsInfoSql, (err, results) => {
        if(err){
            return res.send({
                status: 404,
                message: '未知错误！请稍候重试！'
            })
        }

        if(results.length <= 0){
            return res.send({
                status: 404,
                message: '暂无数据！'
            })
        }

        res.send({
            status: 200,
            message: '获取成功！',
            data: results
        })
    })
}


// 获取所有商品信息
function getAllgoodsInfo(req, res){
    // 定义一个查询所有商品的  SQL
    const getAllgoodsInfoSql = 'select * from goods'

    // 执行 SQL 
    dataBase.query(getAllgoodsInfoSql, (err, results) => {
        if(err){
            return res.send({
                status: 404,
                message: '未知错误！请稍候重试！'
            })
        }

        if(results.length <= 0){
            return res.send({
                status: 404,
                message: '暂无数据！'
            })
        }

        res.send({
            status: 200,
            message: '获取成功！',
            data: results
        })
    })
}


// 获取商品的类别
function getGoodsCategory(req, res){
    // 定义一个查询所有商品的  SQL
    const getAllgoodsInfoSql = 'select goodstype from goods group by goodstype'

    // 执行 SQL 
    dataBase.query(getAllgoodsInfoSql, (err, results) => {
        if(err){
            return res.send({
                status: 404,
                message: '未知错误！请稍候重试！'
            })
        }

        if(results.length <= 0){
            return res.send({
                status: 404,
                message: '暂无数据！'
            })
        }

        res.send({
            status: 200,
            message: '获取成功！',
            data: results
        })
    })
}


// 获取管理员自己上传的商品
function getAdministratorGoods(req, res){
    const getAdministratorGoodsSql = 'select * from goods where AddActor = ?'


    dataBase.query(getAdministratorGoodsSql, req.body.username, (err, results) => {
        if(err){
            return res.send({
                status: 404,
                message: '未知错误！请稍候重试！'
            })
        }
        if(results.length <= 0){
            return res.send({
                status: 404,
                message: '暂无数据！'
            })
        }
        res.send({
            status: 200,
            message: '获取成功！',
            data: results
        })
    })
}


// 当用户查看商品后增加商品的点击量
function addNumberHits(req, res){
    // 定义一条添加点击量的 SQL
    const addNumberHitsSql = 'update goods set PurchaseQuantity = PurchaseQuantity + 1 where id = ?';

    // 执行 SQL
    dataBase.query(addNumberHitsSql, req.body.id, (err, results) => {
        if(err){
            return res.send({
                status: 404,
                message: '未知错误！请稍后重试！'
            })
        }

        if(results.affectedRows !== 1){
            return res.send({
                status: 404,
                message: '未知错误！请稍后重试！'
            })
        }

        res.send({
            status: 200,
            message: '点击量增加成功！'
        })
    })
}


module.exports = {
    addGoods,
    getPopularGoods,
    getAllgoodsInfo,
    getBigGoodsInfo,
    getGoodsCategory,
    getAdministratorGoods,
    addNumberHits
}